﻿using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Grid;
using RecruitStudents.Client.BLL;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Score
{
    public partial class Score_ScoreUpForm_List : BaseForm
    {
        private DataTable dtExport;
        ServiceScore serviceScore = new ServiceScore();
        ServiceComm serviceComm = new ServiceComm();
        public Score_ScoreUpForm_List()
        {
            InitializeComponent();
            this.cmbExamRoom.Properties.DataSource = null;
            this.cmbTeacher.Properties.DataSource = null;
            this.cmbTeacher.Properties.Columns.Clear();
            this.cmbTeacher.Properties.NullText = "";
            BindExamRoom();
        }
        private void BindExamRoom()
        {
            string strExaminationID = SessionerHelper.ExaminationID;
            using (DataTable dt = serviceComm.GetExamRoomTableByEntretenimientohID(strExaminationID))
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow drNew = dt.NewRow();
                    drNew["ExamRoomNO"] = "";
                    drNew["ExamRoomName"] = "全部";
                    dt.Rows.InsertAt(drNew, 0);
                }
                this.cmbExamRoom.Properties.NullText = "";
                this.cmbExamRoom.Properties.Columns.Add(new LookUpColumnInfo("ExamRoomNO", "考场代码"));
                this.cmbExamRoom.Properties.Columns.Add(new LookUpColumnInfo("ExamRoomName", "考场名称"));
                this.cmbExamRoom.Properties.DataSource = dt;
                this.cmbExamRoom.Properties.DisplayMember = "ExamRoomName";
                this.cmbExamRoom.Properties.ValueMember = "ExamRoomNO";
                this.cmbExamRoom.Properties.Columns[0].Visible = false;
            }
        }
        private void BindTeacher()
        {
            this.cmbTeacher.Properties.Columns.Clear();
            this.cmbTeacher.Properties.DataSource = null;
            this.cmbTeacher.Properties.NullText = "";
            this.cmbTeacher.EditValue = "";
            if (cmbExamRoom.EditValue == null || cmbExamRoom.EditValue.ToSafeString().Length == 0)
            {
                return;
            }
            this.cmbTeacher.Properties.Columns.Add(new LookUpColumnInfo("LoginName", "考官登录名"));
            this.cmbTeacher.Properties.Columns.Add(new LookUpColumnInfo("ExaminerName", "考官姓名"));
            DataTable dt = serviceComm.GetTeacherList(cmbExamRoom.EditValue.ToSafeString());
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow drNew = dt.NewRow();
                drNew["LoginName"] = "";
                drNew["ExaminerName"] = "全部";
                dt.Rows.InsertAt(drNew, 0);
            }
            this.cmbTeacher.Properties.DataSource = dt;
            this.cmbTeacher.Properties.DisplayMember = "ExaminerName";
            this.cmbTeacher.Properties.ValueMember = "LoginName";
            this.cmbTeacher.Properties.Columns[0].Visible = false;
        }
        private void cmbExamRoom_EditValueChanged(object sender, EventArgs e)
        {
            BindTeacher();
            BindDataGrid();
        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            this.BindDataGrid();
        }
        private void BindDataGrid()
        {
            this.dgvQD.DataSource = null;
            string strExamRoomID = string.Empty;
            if (cmbExamRoom.EditValue != null && !string.IsNullOrEmpty(cmbExamRoom.EditValue.ToString()))
            {
                strExamRoomID = cmbExamRoom.EditValue.ToString().Trim();
            }
            string strTeacher = string.Empty;
            if (this.cmbTeacher.EditValue != null)
            {
                strTeacher = this.cmbTeacher.EditValue.ToString();
            }
            string strSFYSB = "1";
            using (DataTable dt = serviceScore.GetScoreQuery(strSFYSB, strExamRoomID, strTeacher, SessionerHelper.ExaminationID, SessionerHelper.ExamPlaceID, SessionerHelper.SpecialityKindsNO, SessionerHelper.ExamLevel))
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    this.dgvQD.DataSource = dt;
                    for (int i = 0; i < gridView1.Rows.Count; i++)
                    {
                        if (gridView1.Rows[i].DataRow["IsPass"].ToSafeString() == "1")
                        {
                            //gridView1.Rows[i].DefaultRowStyle = new DefaultRowStyle(gridView1, i) { BackColor = Color.Red };
                            gridView1.SelectRow(i);
                        }
                        else
                        {
                            gridView1.UnselectRow(i);
                        }
                    }
                    dtExport = dt.Copy();
                    if (dt.Select("IsPublish='1'").Length > 0)
                    {
                        this.gridView1.ReadOnly = true;
                        this.btnScoreUp.Enabled = false;
                    }
                }
                else
                {
                    dtExport = null;
                }
            }
        }
        private void cmbTeacher_EditValueChanged(object sender, EventArgs e)
        {
            if (cmbExamRoom.EditValue != null || !string.IsNullOrEmpty(cmbExamRoom.EditValue.ToString()))
            {
                this.BindDataGrid();
            }
        }
        private void btnExport_Click(object sender, EventArgs e)
        {
            this.BindDataGrid();
            if (dtExport != null && dtExport.Rows.Count > 0)
            {
                SaveFileDialog kk = new SaveFileDialog();
                kk.Title = "保存EXECL文件";
                kk.Filter = "EXECL文件(*.xls) |*.xls |所有文件(*.*) |*.*";
                kk.FilterIndex = 1;
                kk.FileName = "考生成绩数据导出" + DateTime.Now.ToString("yyyyMMdd");
                kk.ShowDialog();
                if (kk.FileName.IndexOf(":") < 0) return; //被点了取消 

                string saveFileName = kk.FileName;
                string title = DateTime.Now.Year.ToSafeString() + "年" + SessionerHelper.SystemSchoolName + "招生考试";

                NPOIHelper.ListColumnsName = new SortedList(new NoSort());
                NPOIHelper.ListColumnsName.Add("StudentName", "姓名");
                NPOIHelper.ListColumnsName.Add("Sex", "性别");
                NPOIHelper.ListColumnsName.Add("ExamProvinceName", "生源地");
                NPOIHelper.ListColumnsName.Add("LotNO", "抽签号");
                NPOIHelper.ListColumnsName.Add("AllowExamID", "准考证号");
                NPOIHelper.ListColumnsName.Add("CreateTime", "考试时间");
                NPOIHelper.ListColumnsName.Add("ExamRoomName", "考场名称");

                NPOIHelper.ListColumnsName.Add("Score1", "评分1");
                NPOIHelper.ListColumnsName.Add("Score2", "评分2");
                NPOIHelper.ListColumnsName.Add("Score3", "评分3");
                NPOIHelper.ListColumnsName.Add("Score4", "评分4");
                NPOIHelper.ListColumnsName.Add("Score5", "评分5");
                NPOIHelper.ListColumnsName.Add("Score6", "评分6");
                NPOIHelper.ListColumnsName.Add("Score7", "评分7");
                NPOIHelper.ListColumnsName.Add("TatolScore", "综合评分");
                //NPOIHelper.ListColumnsName.Add("IsPass", "是否通过");
                NPOIHelper.ExportExcel(dtExport, saveFileName, title);
            }
            else
            {
                DevHelper.ShowErrorMessageBox("暂时还没有考生成绩,无法进行导出!");
            }
        }
        private bool GetSelectRowsValue(out string strAllowExamID, out string strIsPass)
        {
            strAllowExamID = string.Empty;
            strIsPass = string.Empty;
            GridRowCollection gridRows = gridView1.SelectedRows;
            if (gridRows != null && gridRows.Count > 0)
            {
                for (int i = 0; i < gridView1.Rows.Count; i++)
                {
                    string strAllowExamID_New = gridView1.Rows[i].DataRow["AllowExamID"].ToString();
                    bool result = false;
                    foreach (GridRow dr in gridRows)
                    {
                        if (strAllowExamID_New == dr.DataRow["AllowExamID"].ToSafeString())
                        {
                            result = true;
                            break;
                        }
                    }
                    if (result)
                    {
                        if (strAllowExamID.Length == 0)
                        {
                            strAllowExamID = gridView1.Rows[i].DataRow["AllowExamID"].ToString();
                            strIsPass = "1";
                        }
                        else
                        {
                            strAllowExamID += "," + gridView1.Rows[i].DataRow["AllowExamID"].ToString();
                            strIsPass += ",1";
                        }

                    }
                    else
                    {
                        if (strAllowExamID.Length == 0)
                        {
                            strAllowExamID = gridView1.Rows[i].DataRow["AllowExamID"].ToString();
                            strIsPass = "0";
                        }
                        else
                        {
                            strAllowExamID += "," + gridView1.Rows[i].DataRow["AllowExamID"].ToString();
                            strIsPass += ",0";
                        }
                    }
                }
                return true;
            }
            else
            {
                DevHelper.ShowInfoMessageBox("您还没有选择考生数据,无法进行上报!");
                return false;
            }
        }
        private void btnScoreUp_Click(object sender, EventArgs e)
        {
            try
            {
                if (XtraMessageBox.Show("请确认是否上报?", "上报确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                {
                    string strAllowExamID = string.Empty;
                    string strIsPass = string.Empty;
                    if (GetSelectRowsValue(out strAllowExamID, out strIsPass))
                    {
                        string strExamLevel = SessionerHelper.ExamLevel;
                        string strExamPlaceID = SessionerHelper.ExamPlaceID;
                        string strSpecialityKindsNO = SessionerHelper.SpecialityKindsNO;
                        string strMessage = serviceScore.SaveUpData(strAllowExamID, strSpecialityKindsNO, strExamLevel, strExamPlaceID, strIsPass);
                        if (strMessage.Length == 0)
                        {
                            DevHelper.ShowInfoMessageBox("上报成绩成功!");
                            BindDataGrid();
                        }
                        else
                        {
                            DevHelper.ShowErrorMessageBox("上报成绩出现错误:" + strMessage);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DevHelper.ShowErrorMessageBox("上报成绩出现错误:" + ex.Message);
            }
        }
        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.GetFocusedValue() != null)
            {
                GridView rows = sender as GridView;
                int[] inteurm = rows.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    Score_ScoreUpForm_Option option = new Score_ScoreUpForm_Option();
                    option.strAllowExamID = rows.Rows[inteurm[0]].DataRow["AllowExamID"].ToString();
                    option.strSpecialityKindsNO = rows.Rows[inteurm[0]].DataRow["SpecialityKindsNO"].ToString();
                    option.strExamLevel = rows.Rows[inteurm[0]].DataRow["ExamLevel"].ToString();
                    option.ShowForm();
                }
            }
        }
        private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (gridView1.GetRow(e.RowHandle) == null)
            {
                return;
            }
            else
            {
                //获取所在行指定列的值
                string state = gridView1.GetRowCellValue(e.RowHandle, "IsPass").ToString();
                //比较指定列的状态
                if (state == "1")
                {
                    e.Appearance.BackColor = Color.Red;//设置此行的背景颜色
                }
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            gridView1.IndicatorWidth = 40;
            gridView1.PreviewIndent = 0;
            gridView1.CustomDrawRowIndicator += gridView1_CustomDrawRowIndicator;
            gridView1.CustomDrawCell += gridView1_CustomDrawCell;
            base.OnLoad(e);
        }
        void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = HorzAlignment.Far;
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }
    }
}
